package Thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: homechen
 * Date: 2024-03-14
 * Time: 9:51
 */

/**
 * 懒汉模式
 */
public class Singleton {
    public static volatile Singleton singleton = null;

    private Singleton() {

    }

    public static Singleton getInstance() {
        if (singleton == null) { // 保证等待锁的消耗
            synchronized (Singleton.class) { // 加锁保证多线程只有一个线程进入
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}
